<?php

declare(strict_types=1);

namespace App\Controllers;

use Core\Controller;

class Install extends Controller1
{
    /**
     * 初始化数据库表结构（论坛核心表）
     */
    public function init(): void
    {
        try {
            // 启用外键约束
            $this->app->query("PRAGMA foreign_keys = ON;");

            // 创建 users 表
            $this->app->query("
                CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    username TEXT NOT NULL UNIQUE CHECK(length(username) >= 3),
                    password TEXT NOT NULL,
                    email TEXT UNIQUE,
                    avatar TEXT DEFAULT 'default.jpg',
                    bio TEXT,
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    last_login DATETIME
                )
            ");

            // 创建 forums 表（板块）
            $this->app->query("
                CREATE TABLE IF NOT EXISTS forums (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL UNIQUE,
                    description TEXT,
                    sort_order INTEGER DEFAULT 0,
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
                )
            ");

            // 创建 posts 表（帖子）
            $this->app->query("
                CREATE TABLE IF NOT EXISTS posts (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL,
                    forum_id INTEGER NOT NULL,
                    title TEXT NOT NULL CHECK(length(title) >= 5),
                    content TEXT NOT NULL,
                    view_count INTEGER DEFAULT 0,
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
                    FOREIGN KEY (forum_id) REFERENCES forums(id) ON DELETE CASCADE
                )
            ");

            // 创建 comments 表（评论）
            $this->app->query("
                CREATE TABLE IF NOT EXISTS comments (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    post_id INTEGER NOT NULL,
                    user_id INTEGER NOT NULL,
                    content TEXT NOT NULL CHECK(length(content) >= 3),
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
                    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
                )
            ");

            // 创建初始板块数据
            $this->app->query("
                INSERT OR IGNORE INTO forums (name, description, sort_order) 
                VALUES 
                    ('默认板块', '系统默认板块', 1),
                    ('技术讨论', '编程、技术相关讨论', 2),
                    ('生活闲聊', '日常话题交流区', 3)
            ");

            $this->app->json([
                'success' => true,
                'message' => '数据库初始化成功！已创建用户、板块、帖子、评论表',
                'tables' => ['users', 'forums', 'posts', 'comments']
            ]);
        } catch (\Throwable $e) {
            $this->app->json([
                'success' => false,
                'error' => '数据库初始化失败: ' . $e->getMessage()
            ], 500);
        }
    }
}
